import { _decorator, Component, director, Event, input, Label, Node, resources, Size, Sprite, SpriteFrame, UITransform, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

interface IGoods {
  icon: string,
  name: string,
  id: string,
  price?: number
}

@ccclass('GoodsControl')
export class GoodsControl extends Component {

  goods: IGoods[] = [
    {
      icon: 'shoulijian',
      name: '手里剑',
      id: '001',
      price: 50
    },
    {
      icon: 'flashwepen',
      name: '闪电鞭',
      id: '002',
      price: 50
    }
  ]
  //X基线
  @property(Number)
  baseX: number = -308
  //x间隔
  @property(Number)
  spaceX: number = 174
  //Y基线
  @property(Number)
  baseY: number = 146
  chooseList: Object[] = []
  //弹窗
  window: Node = null
  start() {

  }

  update(deltaTime: number) {

  }
  back() {
    director.loadScene('scene')
  }
  createGoodsNode(goodObj: IGoods) {
    const index = this.goods.findIndex(item => item.id === goodObj.id)
    const skewX = index * this.spaceX + this.baseX

    // 动态加载商品
    const goods = new Node()
    goods.name = 'goods' + goodObj.id
    goods.setPosition(new Vec3(skewX, this.baseY, 0))
    // goods.addComponent(UITransform).setContentSize(new Size(97, 97))
    goods.addComponent(Sprite)
    resources.load(goodObj.icon, SpriteFrame, null, (e, frame) => {
      goods.getComponent(Sprite).spriteFrame = frame
    })
    const label = new Node()
    goods.addChild(label)
    label.addComponent(Label).string = goodObj.name
    label.setPosition(new Vec3(1.283, -65.455, 0))
    this.node.addChild(goods)
  }
  choose(params: any) {
    const name = this.goods.find(item => item.id === params.target.name).name
    this.chooseList = [name]
    this.window = this.node.getParent().getChildByName('window')
    this.window.getChildByName('title').getComponent(Label).string = `确定使用 ${name} 作为副武器吗？`
    this.window.active = true
  }
  handleWindow(params: any) {
    console.log(params);
    this.window.active = false
  }
}


